package cn.cntv.app.ipanda;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;
import cn.cntv.app.ipanda.cctv.fragment.CCTVFragment;
import cn.cntv.app.ipanda.constant.Constants;
import cn.cntv.app.ipanda.home.fragment.HomeFragment;
import cn.cntv.app.ipanda.livechina.fragment.LiveChinaFragment;
import cn.cntv.app.ipanda.lovepanda.fragment.LovePandaFragment;
import cn.cntv.app.ipanda.pandaeye.fragment.PandaEyeFragment;

public class MainActivity extends BaseActivity {

	static final String key_selected_index = "select_index";

	private static final String TAG = "@@@MainActivity";

	/**
	 * 用于对 Fragment 进行管理
	 */
	private FragmentManager mFragmentManager;

	/**
	 * 当前选中的Fragment索引
	 */
	private int mSelectedFragmentIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		mFragmentManager = getSupportFragmentManager();

		if (savedInstanceState != null) {

			mSelectedFragmentIndex = savedInstanceState.getInt(
					key_selected_index, 0);
			// 需要设置view的选中状态
			// 需要设置标题的显示状态
		}

		initViews();

		setCurrentFragment(mSelectedFragmentIndex);
	}

	/**
	 * 加载控件
	 */
	private void initViews() {

		RadioGroup radioGroup = (RadioGroup) findViewById(R.id.tabGroup);

		radioGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						onTabBtnClick(checkedId);
					}
				});
	}

	/**
	 * 设置当前选中的Fragment
	 * 
	 * @param fragmentIndex
	 *            选中Fragment的索引
	 */
	private void setCurrentFragment(int fragmentIndex) {

		FragmentTransaction transaction = mFragmentManager.beginTransaction();
		hiddenAllTabFragment(transaction);

		String fragmentTag = Constants.RootTags[fragmentIndex];
		Fragment fragment = mFragmentManager.findFragmentByTag(fragmentTag);

		if (fragment == null) {

			switch (fragmentIndex) {

			case Constants.root_fragment_select_index_home: // 跳转到首页面

				fragment = HomeFragment.newInstance();

				break;

			case Constants.root_fragment_select_index_panda: // 跳转到爱熊猫页面

				fragment = LovePandaFragment.newInstance();

				break;
			case Constants.root_fragment_select_index_chinaonline: // 跳转到直播中国页面

				fragment = LiveChinaFragment.newInstance();

				break;

			case Constants.root_fragment_select_index_cctv: // 跳转到CCTV页面

				fragment = CCTVFragment.newInstance();
				break;

			case Constants.root_fragment_select_index_pandaeye: // 跳转到熊猫眼页面

				fragment = PandaEyeFragment.newInstance();
				break;
			}

			transaction.add(R.id.flHomeContent, fragment, fragmentTag);
		} else {

			transaction.show(fragment);
		}

		transaction.commitAllowingStateLoss();
		mSelectedFragmentIndex = fragmentIndex;
	}

	/**
	 * tab 样式activity 隐藏所有fragment
	 */
	public void hiddenAllTabFragment(FragmentTransaction transaction) {

		Fragment fragment;

		for (String tag : Constants.RootTags) {

			fragment = mFragmentManager.findFragmentByTag(tag);

			if (fragment != null) {

				transaction.hide(fragment);
			}
		}
	}

	/**
	 * RadioButton点击
	 * 
	 * @param viewId
	 *            点击的viewId
	 */
	private void onTabBtnClick(int viewId) {

		int tempIndex = 0;
		switch (viewId) {
		case R.id.rbTabHome:
			tempIndex = Constants.root_fragment_select_index_home;
			break;
		case R.id.rbTabLovePanda:
			tempIndex = Constants.root_fragment_select_index_panda;
			break;
		case R.id.rbTabLiveChina:
			tempIndex = Constants.root_fragment_select_index_chinaonline;
			break;
		case R.id.rbTabCCTV:
			tempIndex = Constants.root_fragment_select_index_cctv;
			break;
		case R.id.rbTabPandaEye:
			tempIndex = Constants.root_fragment_select_index_pandaeye;
			break;
		}
		if (tempIndex == mSelectedFragmentIndex) {

			return;
		} else {

			setCurrentFragment(tempIndex);
		}

	}

	public void onSaveInstanceState(Bundle outState) {

		outState.putInt(key_selected_index, mSelectedFragmentIndex);
		super.onSaveInstanceState(outState);
	}
}
